<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 店铺管理
 *
 * @icon fa fa-circle-o
 */
class Shop extends Backend
{
    
    /**
     * Shop模型对象
     * @var \app\admin\model\Shop
     */
    protected $model = null;
    protected $searchFields = 'name';
    protected $noNeedLogin = ['detail','getOrderListByConsumerId'];

    // 数据限制
    protected $dataLimit = 'personal';
    protected $dataLimitField = 'dealer_ids';
    protected $dataLimitFieldAutoFill = false;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Shop;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                $dealer_id = $this->getDataLimitAdminIds();
                $where_sql['shop_id'] = $row['shop_id'];
                $where_sql['status'] = ['<>','7'];
                $where_sql['dealer_id'] = ['in',$row['dealer_ids']];
                $where_sql['head_id'] = ['<>',0];
                $row['orders'] = Db::name('order')->where($where_sql)->count();
                $row['total'] = Db::name('order')->where($where_sql)->sum('money');
                $row->visible(['id','shop_id','name','granttime','orders','total']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 详情
     */
    public function detail($shop_id = '')
    {
        $data = Db::name('shop')
            ->where('shop_id', $shop_id)
            ->field('id, shop_id, name, granttime, dealer_ids')
            ->find();
        // 当前登录用户所属的经销商
        $dealer_id = $this->getDataLimitAdminIds();
        if($dealer_id){
            $where_total['dealer_id'] = $dealer_id;
            $where_dis['dealer_id'] = $dealer_id;
            $where_order['o.dealer_id'] = $dealer_id;
        }else{
            $where_total['dealer_id'] = ['in', $data['dealer_ids']];
            $where_dis['dealer_id'] = ['in', $data['dealer_ids']];
            $where_order['o.dealer_id'] = ['in', $data['dealer_ids']];
        }

//        dump($dealer_id);exit();
        $dealer = Db::name('dealer')->where('id', 'in', $data['dealer_ids'])->column('name');
        $data['dealername'] = implode(',', $dealer);

        $where_total['shop_id'] = $shop_id;
        $where_total['status'] = ['<>','7'];
        $where_total['head_id'] = ['<>',0];
        $data['total'] = Db::name('order')->where($where_total)->sum('money');

        // 分销商列表
        $where_dis['shop_id'] = $shop_id;

        $where_dis['head_id'] = ['<>',0];
        $consumer_id_arr = Db::name('order')->where($where_dis)->distinct(true)->column('consumer_id');
        $consumer_ids = implode(',', $consumer_id_arr);
        $consumer_list = Db::name('consumer')->where('id', 'in', $consumer_ids)->column('id,name');

        // 分销商订单
        $where_order['o.shop_id'] = $shop_id;

        $where_order['o.status'] = ['<>','7'];
        $where_order['o.head_id'] = ['<>',0];
        $orders = Db::name('order')
            ->alias('o')
            ->join('dealer d','o.dealer_id = d.id')
            ->where($where_order)
            ->field(
                'o.ordernum, o.goods, o.ordertime, o.money, o.receiver, o.phone, o.address,
                 d.name as dealername'
            )
            ->select();
//            ->paginate(10);
//        dump($orders);exit();

        $this->assign('data', $data);
        $this->assign('consumer_list', $consumer_list);
        $this->assign('orders', $orders);

        return $this->view->fetch();
    }

    /**
     * 获取指定分销用户的订单
     */
    public function getOrderListByConsumerId()
    {
        $shop_id = input('shop_id');
        $consumer_id = input('consumer_id');
        $where_sql['o.shop_id'] = $shop_id;
        $where_sql['o.head_id'] = ['<>',0];
        $where_sql['o.status'] = ['<>','7'];
        if($consumer_id){
            $where_sql['o.consumer_id'] = $consumer_id;
        }
        $orders = Db::name('order')
            ->alias('o')
            ->join('dealer d','o.dealer_id = d.id')
            ->where($where_sql)
            ->field(
                'o.ordernum, o.goods, o.ordertime, o.money, o.receiver, o.phone, o.address,
                 d.name as dealername'
            )
            ->select();

        if($orders){
            $result = array(
                'status' => 1,
                'orders' => $orders
            );
        }else{
            $result = array(
                'status' => 0,
            );
        }

        return($result);
    }
}
